package com.cuiweiyou.bluemesh.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cuiweiyou.bluemesh.R;

public class EditTextDialog {
    private EditTextDialog() {
    }

    public static void createDialog(final Context context, final OnEditTextDialogDismissListener listener) {

        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        View view = View.inflate(context, R.layout.dialog_edit, null);
        final EditText nickNameEditText = (EditText) view.findViewById(R.id.nickNameEditText);
        Button confirmButton = (Button) view.findViewById(R.id.confirmButton);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nickNameEditText.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(context, "请输入名称", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (null != listener) {
                    dialog.dismiss();
                    listener.onEditTextDialogDismiss(name);
                }
            }
        });

        dialog.setView(view);
        dialog.show();
    }

    public static interface OnEditTextDialogDismissListener {
        public void onEditTextDialogDismiss(String nickname);
    }
}
